' This file implements the code-behind class for SelectFileToImport.aspx.
' App_Code\FirstPage.Controls.vb contains the Table, Row and Record control classes
' for the page.  Best practices calls for overriding methods in the Row or Record control classes.

#region "Imports statements"

Imports System
Imports System.Data
Imports System.Collections
Imports System.ComponentModel
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports BaseClasses
Imports BaseClasses.Utils
Imports BaseClasses.Data
Imports BaseClasses.Data.SqlProvider
Imports BaseClasses.Web.UI.WebControls
Imports Training1.Business
Imports Training1.Data

#end region

Namespace Training1.UI
    
    
    Class SelectFileToImport
        Inherits BaseApplicationPage
#Region "Section 1: Place your customizations here."    
        
        Public Sub New()
            MyBase.New
            Me.Initialize
            Me.Page.Title = Me.GetResourceValue("Import:Step1", Me.AppName)
        End Sub
        
        Public ReadOnly Property TableName() As String
            Get
                Return Me.Decrypt(BaseClasses.Utils.NetUtils.GetUrlParam(Me, "TableName", True))
            End Get
        End Property
        Public ReadOnly Property AppName() As String
            Get
                Return BaseClasses.Configuration.ApplicationSettings.Current.GetAppSetting(BaseClasses.Configuration.ApplicationSettings.ConfigurationKey.ApplicationName)
            End Get
        End Property
        
        Public Sub LoadData()
            ' LoadData reads database data and assigns it to UI controls.
            ' Customize by adding code before or after the call to LoadData_Base()
            ' or replace the call to LoadData_Base().
            LoadData_Base
            Me.InfoLabel.Text = Me.GetResourceValue("Import:InfoText", Me.AppName)
        End Sub
#region "Ajax Functions"
        
        <System.Web.Services.WebMethod()>  _
        Public Shared Function GetRecordFieldValue(ByVal tableName As String, ByVal recordID As String, ByVal columnName As String, ByVal title As String, ByVal persist As Boolean, ByVal popupWindowHeight As Integer, ByVal popupWindowWidth As Integer, ByVal popupWindowScrollBar As Boolean) As Object()
            ' GetRecordFieldValue gets the pop up window content from the column specified by
            ' columnName in the record specified by the recordID in data base table specified by tableName.
            ' Customize by adding code before or after the call to  GetRecordFieldValue_Base()
            ' or replace the call to  GetRecordFieldValue_Base().
            Return GetRecordFieldValue_Base(tableName, recordID, columnName, title, persist, popupWindowHeight, popupWindowWidth, popupWindowScrollBar)
        End Function
        
        <System.Web.Services.WebMethod()>  _
        Public Shared Function GetImage(ByVal tableName As String, ByVal recordID As String, ByVal columnName As String, ByVal title As String, ByVal persist As Boolean, ByVal popupWindowHeight As Integer, ByVal popupWindowWidth As Integer, ByVal popupWindowScrollBar As Boolean) As Object()
            ' GetImage gets the Image url for the image in the column "columnName" and
            ' in the record specified by recordID in data base table specified by tableName.
            ' Customize by adding code before or after the call to  GetImage_Base()
            ' or replace the call to  GetImage_Base().
            Return GetImage_Base(tableName, recordID, columnName, title, persist, popupWindowHeight, popupWindowWidth, popupWindowScrollBar)
        End Function
        
#end region
        ' Page Event Handlers - buttons, sort, links
        Public Sub NextButton_Click(ByVal sender As Object, ByVal args As EventArgs)
            NextButton_Click_Base(sender, args)
        End Sub

#End region

#Region "Section 2: Do not modify this section."
        
        Private Sub Initialize()
            ' Called by the class constructor to initialize event handlers for Init and Load
            ' You can customize by modifying the constructor in Section 1.
            AddHandler Init, AddressOf Me.Page_InitializeEventHandlers
            AddHandler Load, AddressOf Me.Page_Load
        End Sub
        
        ' Handles base.Init. Registers event handler for any button, sort or links.
        ' You can add additional Init handlers in Section 1.
        Protected Overridable Sub Page_InitializeEventHandlers(ByVal sender As Object, ByVal e As System.EventArgs)
            ' Register the Event handler for any Events.

            AddHandler NextButton.Button.Click, AddressOf Me.NextButton_Click

        End Sub
        
        ' Handles base.Load.  Read database data and put into the UI controls.
        ' You can add additional Load handlers in Section 1.
        Protected Overridable Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Check if user has access to this page.  Redirects to either sign-in page
            ' or 'no access' page if not. Does not do anything if role-based security
            ' is not turned on, but you can override to add your own security.
            Me.Authorize(Me.GetAuthorizedRoles)
            ' Load data only when displaying the page for the first time
            If Not Me.IsPostBack Then
                ' Setup the header text for the validation summary control.
                Me.ValidationSummary1.HeaderText = GetResourceValue("ValidationSummaryHeaderText", Me.AppName)
                ' Read the data for all controls on the page.
                ' To change the behavior, override the DataBind method for the individual
                ' record or table UI controls.
                Me.LoadData()
               
            End If
        End Sub
        
        Public Shared Function GetRecordFieldValue_Base(ByVal tableName As String, ByVal recordID As String, ByVal columnName As String, ByVal title As String, ByVal persist As Boolean, ByVal popupWindowHeight As Integer, ByVal popupWindowWidth As Integer, ByVal popupWindowScrollBar As Boolean) As Object()
            Dim content As String = NetUtils.EncodeStringForHtmlDisplay(BaseClasses.Utils.MiscUtils.GetFieldData(tableName, recordID, columnName))
            ' returnValue is an array of string values.
            ' returnValue(0) represents title of the pop up window.
            ' returnValue(1) represents content ie, image url.
            ' returnValue(2) represents whether pop up window should be made persistant
            ' or it should close as soon as mouse moves out.
            ' returnValue(3), (4) represents pop up window height and width respectivly
            ' returnValue(5) represents whether pop up window should contain scroll bar.
            ' (0),(2),(3) and (4) is initially set as pass through attribute.
            ' They can be modified by going to Attribute tab of the properties window of the control in aspx page.
            Dim returnValue() As Object = New Object((6) - 1) {}
            returnValue(0) = title
            returnValue(1) = content
            returnValue(2) = persist
            returnValue(3) = popupWindowWidth
            returnValue(4) = popupWindowHeight
            returnValue(5) = popupWindowScrollBar
            Return returnValue
        End Function
        
        Public Shared Function GetImage_Base(ByVal tableName As String, ByVal recordID As String, ByVal columnName As String, ByVal title As String, ByVal persist As Boolean, ByVal popupWindowHeight As Integer, ByVal popupWindowWidth As Integer, ByVal popupWindowScrollBar As Boolean) As Object()
            Dim content As String = ("<IMG src =" + ("\""../Shared/ExportFieldValue.aspx?Table="  _
                        + (tableName + ("&Field="  _
                        + (columnName + ("&Record="  _
                        + (recordID + "\""/>")))))))
            ' returnValue is an array of string values.
            ' returnValue(0) represents title of the pop up window.
            ' returnValue(1) represents content ie, image url.
            ' returnValue(2) represents whether pop up window should be made persistant
            ' or it should close as soon as mouse moves out.
            ' returnValue(3), (4) represents pop up window height and width respectivly
            ' returnValue(5) represents whether pop up window should contain scroll bar.
            ' (0),(2),(3), (4) and (5) is initially set as pass through attribute.
            ' They can be modified by going to Attribute tab of the properties window of the control in aspx page.
            Dim returnValue() As Object = New Object((6) - 1) {}
            returnValue(0) = title
            returnValue(1) = content
            returnValue(2) = persist
            returnValue(3) = popupWindowWidth
            returnValue(4) = popupWindowHeight
            returnValue(5) = popupWindowScrollBar
            Return returnValue
        End Function
        
        ' Load data from database into UI controls.
        ' Modify LoadData in Section 1 above to customize.  Or override DataBind() in
        ' the individual table and record controls to customize.
        Public Sub LoadData_Base()
            
        End Sub

        ' event handler for Button with Layout
        Public Sub NextButton_Click_Base(ByVal sender As Object, ByVal args As EventArgs)
            Dim inputFile As System.Web.UI.HtmlControls.HtmlInputFile
            Dim tmpPath As String
            inputFile = CType(Me.Page.FindControl("inputFile"), System.Web.UI.HtmlControls.HtmlInputFile)
            If (Not (inputFile.PostedFile Is Nothing) _
                        AndAlso (inputFile.PostedFile.ContentLength > 0)) Then
                If IsFileTypeSupported(inputFile.PostedFile.FileName) Then
                    Try
                        tmpPath = System.IO.Path.GetTempFileName()
                        inputFile.PostedFile.SaveAs(tmpPath)
                        Me.Page.Session("FilePath") = tmpPath

                    Catch ex As Exception
                        Dim tempDir As String = AppDomain.CurrentDomain.BaseDirectory + "~temp"
                        Try
                            If Not Directory.Exists(tempDir) Then
                                Directory.CreateDirectory(tempDir)
                            End If
                        Catch ex1 As Exception
                            If Directory.Exists(tempDir) Then
                                Throw New Exception("Fail to create " + tempDir + ". " + ex.Message)
                            End If
                        End Try
                        Try
                            Dim info As New DirectoryInfo(tempDir)
                            info.Attributes = FileAttributes.Hidden
                            'do nothing 
                        Catch ex2 As Exception
                        End Try
                        
                    End Try

                    ' pass the table name with encryption.
                    Me.Page.Response.Redirect(("ImportData.aspx?TableName=" + Me.Encrypt(Me.TableName)))
                Else
                    BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", Me.GetResourceValue("Import:ValidFileMsg", Me.AppName))
                End If
            Else
                BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", Me.GetResourceValue("Import:SelectFile", Me.AppName))
            End If
        End Sub
        
#End Region

    End Class
End Namespace

